<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="yangbo.wang"><meta name="copyright" content="yangbo.wang"><meta name="generator" content="Hexo 5.3.0"><meta name="theme" content="hexo-theme-yun"><title>HttpReqeust对象 | Blog</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.22/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_ed8vp4atwoj.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/yun.svg"><link rel="mask-icon" href="/yun.svg" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/js/utils.js" as="script"><link rel="preload" href="/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"example.com","root":"/","title":"你的月亮我的心","version":"1.4.0","mode":"dark","copycode":true,"page":{"isPost":true},"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><link rel="stylesheet" href="/css/hexo-theme-yun.css"><script src="/js/utils.js"></script><script src="/js/hexo-theme-yun.js"></script><meta name="description" content="HttpReqeust对象 服务器接收到http协议的请求后，会根据报文创建HttpRequest对象   视图函数的第一个参数是HttpRequest对象   在django.http模块中定义了HttpRequest对象的API    属性 下面除非特别说明，属性都是只读的   path：一个字符串，表示请求的页面的完整路径，不包含域名   method：一个字符串，表示请求使用的HTTP方法">
<meta property="og:type" content="article">
<meta property="og:title" content="HttpReqeust对象">
<meta property="og:url" content="http://example.com/2021/01/17/request%E5%AF%B9%E8%B1%A1/index.html">
<meta property="og:site_name" content="Blog">
<meta property="og:description" content="HttpReqeust对象 服务器接收到http协议的请求后，会根据报文创建HttpRequest对象   视图函数的第一个参数是HttpRequest对象   在django.http模块中定义了HttpRequest对象的API    属性 下面除非特别说明，属性都是只读的   path：一个字符串，表示请求的页面的完整路径，不包含域名   method：一个字符串，表示请求使用的HTTP方法">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-17T07:12:04.091Z">
<meta property="article:modified_time" content="2021-01-17T07:30:52.449Z">
<meta property="article:author" content="yangbo.wang">
<meta property="article:tag" content="Django">
<meta name="twitter:card" content="summary"><script src="/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest"></script><script defer src="/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/about/" title="yangbo.wang"><img width="96" loading="lazy" src="/Yun.png" alt="yangbo.wang"></a><div class="site-author-name"><a href="/about/">yangbo.wang</a></div><a class="site-name" href="/about/site.html">Blog</a><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">8</span></a></div><div class="site-state-item"><a href="/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">2</span></a></div><div class="site-state-item"><a href="/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" href="/" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信 A18309169837" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ  1197213754" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://gitee.com/wyb_001" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail 1197213754@qq.com" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/about/" title="关于我" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#HttpReqeust%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.</span> <span class="toc-text">HttpReqeust对象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7"><span class="toc-number">1.1.</span> <span class="toc-text">属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#GET%E5%B1%9E%E6%80%A7"><span class="toc-number">2.</span> <span class="toc-text">GET属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#POST%E5%B1%9E%E6%80%A7"><span class="toc-number">3.</span> <span class="toc-text">POST属性</span></a></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://example.com/2021/01/17/request%E5%AF%B9%E8%B1%A1/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="yangbo.wang"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="Blog"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">HttpReqeust对象</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2021-01-17 15:12:04" itemprop="dateCreated datePublished" datetime="2021-01-17T15:12:04+08:00">2021-01-17</time></div><div class="post-classify"><span class="post-category"> <span class="post-meta-item-icon" style="margin-right:3px;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span><span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/categories/Python/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">Python</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/tags/Django/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Django</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h3 id="HttpReqeust对象"><a href="#HttpReqeust对象" class="headerlink" title="HttpReqeust对象"></a>HttpReqeust对象</h3><ul>
<li>服务器接收到http协议的请求后，会根据报文创建HttpRequest对象  </li>
<li>视图函数的第一个参数是HttpRequest对象  </li>
<li>在django.http模块中定义了HttpRequest对象的API  </li>
</ul>
<h4 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h4><ul>
<li>下面除非特别说明，属性都是只读的  </li>
<li>path：一个字符串，表示请求的页面的完整路径，不包含域名  </li>
<li>method：一个字符串，表示请求使用的HTTP方法，常用值包括：’GET’、’POST’  </li>
<li>encoding：一个字符串，表示提交的数据的编码方式  <ul>
<li>如果为None则表示使用浏览器的默认设置，一般为utf-8  </li>
<li>这个属性是可写的，可以通过修改它来修改访问表单数据使用的编码，接下来对属性的任何访问将使用新的encoding值  </li>
</ul>
</li>
<li>GET：一个类似于字典的对象，包含get请求方式的所有参数  </li>
<li>POST：一个类似于字典的对象，包含post请求方式的所有参数  </li>
<li>FILES：一个类似于字典的对象，包含所有的上传文件  </li>
<li>COOKIES：一个标准的Python字典，包含所有的cookie，键和值都为字符串  </li>
<li>session：一个既可读又可写的类似于字典的对象，表示当前的会话，只有当Django 启用会话的支持时才可用，详细内容见“状态保持”  </li>
</ul>
<h4 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h4><ul>
<li>is_ajax()：如果请求是通过XMLHttpRequest发起的，则返回True  </li>
</ul>
<h3 id="GET属性"><a href="#GET属性" class="headerlink" title="GET属性"></a>GET属性</h3><ul>
<li>QueryDict类型的对象  </li>
<li>包含get请求方式的所有参数  </li>
<li>与url请求地址中的参数对应，位于?后面  </li>
<li>参数的格式是键值对，如key1=value1  </li>
<li>多个参数之间，使用&amp;连接，如key1=value1&amp;key2=value2  </li>
<li>键是开发人员定下来的，值是可变的  </li>
</ul>
<h3 id="POST属性"><a href="#POST属性" class="headerlink" title="POST属性"></a>POST属性</h3><ul>
<li>QueryDict类型的对象  </li>
<li>包含post请求方式的所有参数  </li>
<li>与form表单中的控件对应  </li>
<li>问：表单中哪些控件会被提交？  </li>
<li>答：控件要有name属性，则name属性的值为键，value属性的值为值，构成键值对提交  </li>
<li>对于checkbox控件，name属性一样为一组，当控件被选中后会被提交，存在一键多值的情况  </li>
<li>键是开发人员定下来的，值是可变的  </li>
</ul>
</div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>yangbo.wang</li><li class="post-copyright-link"><strong>Post link: </strong><a href="http://example.com/2021/01/17/request%E5%AF%B9%E8%B1%A1/" title="HttpReqeust对象">http://example.com/2021/01/17/request%E5%AF%B9%E8%B1%A1/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/2021/01/17/Django%E4%BB%8B%E7%BB%8D/" rel="prev" title="Django简介"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">Django简介</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/2021/01/17/001-%E5%85%83%E7%B1%BB/" rel="next" title="Python 元类"><span class="post-nav-text">Python 元类</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2021 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> yangbo.wang</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.3.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.4.0</span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a></div></body></html>